package com.haibei.activity.user;

import java.util.HashMap;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.common.net.NetResult;
import com.common.task.BaseTask;
import com.common.task.NetCallBack;
import com.common.uitl.StringUtils;
import com.common.uitl.Tool;
import com.common.uitl.ValidateTool;
import com.haibei.App;
import com.haibei.R;
import com.haibei.activity.BaseHaibeiActivity;
import com.haibei.adapter.bean.User;
import com.haibei.net.NetInterface;

public class LoginActivity extends BaseHaibeiActivity implements OnClickListener {

	private Button mButtonCancell;
	private Button mButtonLogin;

	private EditText mEditTextUserName;
	private EditText mEditTextPassWord;

	private View mViewLoginGo;
	private BaseTask mBaseTask;
	private final int INT_GO_RETIST1 = 1000;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initView();
	}

	@Override
	public void initView() {
		setContentView(R.layout.activity_login);
		mEditTextUserName = findEditTextById(R.id.editTextUserName);
		mEditTextPassWord = findEditTextById(R.id.editTextPassword);

		mButtonCancell = findButtonById(R.id.buttonCancell);
		mButtonCancell.setOnClickListener(this);
		mViewLoginGo = findViewById(R.id.textViewLoginGo);
		mViewLoginGo.setOnClickListener(this);
		mButtonLogin = findButtonById(R.id.buttonLogin);
		mButtonLogin.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		int vid = v.getId();
		switch (vid) {
		case R.id.buttonCancell:
			backFinish();
			break;
		case R.id.textViewLoginGo:
			Tool.startActivity(LoginActivity.this, RegistActivity1.class);
			break;
		case R.id.buttonLogin:
			if (checkValue()) {
				login();
			}
			break;
		default:
			break;
		}
	}

	private boolean checkValue() {
		if (!ValidateTool.checkEmail(getUserName())) {
			mEditTextUserName.setError("请输入正确的email地址！");
			return false;
		}
		if (StringUtils.isEmpty(getPassword()) || getPassword().length() < 6) {
			mEditTextUserName.setError("请输入密码");
			return false;
		}
		return true;
	}

	private String getUserName() {
		String text = mEditTextUserName.getText().toString();
		return StringUtils.isEmpty(text) ? "" : text;
	}

	private String getPassword() {
		String text = mEditTextPassWord.getText().toString();
		return StringUtils.isEmpty(text) ? "" : text;
	}

	private void login() {
		HashMap<String, String> hmap = new HashMap<String, String>();
		hmap.put("username", getUserName());
		hmap.put("password", getPassword());
		getLoginTask().execute(hmap);
	}

	private BaseTask getLoginTask() {
		if (null != mBaseTask && !mBaseTask.isCancelled()) {
			mBaseTask.cancel(true);
		}
		mBaseTask = new BaseTask(new NetCallBack() {

			@Override
			public void onPreCall() {
				showProgressDialogWithTask(mBaseTask);
			}

			@Override
			public void onFinish(NetResult result) {
				hideProgressDialogWithTask();
				if (null != result) {
					if (result.isSuccess()) {
						User u = (User) result.getData()[0];
						App.getApp().setUser(u);
						Tool.ToastShow(LoginActivity.this, "登录成功！");
						setResult(RESULT_OK);
						finish();
					} else {
						Tool.ToastShow(LoginActivity.this, result.getMessage());
					}
				} else {
					Tool.ToastShow(LoginActivity.this, R.string.error_net);
				}
			}

			@Override
			public NetResult onDoInBack(HashMap<String, String> paramMap) {
				NetResult netResult = null;
				try {
					netResult = NetInterface.login(LoginActivity.this, paramMap);
				} catch (Exception e) {
					e.printStackTrace();
				}

				return netResult;
			}
		});
		return mBaseTask;
	}

	@Override
	public void backFinish() {
		super.backFinish();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == INT_GO_RETIST1) {
			if (resultCode == RESULT_OK) {
				setResult(RESULT_OK);
				finish();
			}
		}
		super.onActivityResult(requestCode, resultCode, data);
	}
}
